package com.example.cc.client.service.impl;

import com.example.cc.client.properties.ConfigCenterProperties;
import com.example.cc.client.service.ClientRegisterService;
import com.example.cc.client.utils.HttpUtil;
import com.example.cc.common.constants.ConfigConstants;
import com.example.cc.common.constants.EndPoint;
import com.example.cc.common.dto.RegisterDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ClientRegisterServiceImpl implements ClientRegisterService {

    private final ConfigCenterProperties configCenterProperties;

    @Autowired
    public ClientRegisterServiceImpl(ConfigCenterProperties configCenterProperties) {
        this.configCenterProperties = configCenterProperties;
    }

    @Override
    public void doRegister() {
        RegisterDTO request = new RegisterDTO(
                configCenterProperties.getClientId(),
                configCenterProperties.getClientName()
        );
        HttpUtil.post(configCenterProperties.getServerUrl() + ConfigConstants.API_PATH + EndPoint.REGISTER, request, Void.class);
    }

    @Override
    public void doUnregister(String clientId) {
        HttpUtil.delete(configCenterProperties.getServerUrl() + ConfigConstants.API_PATH + EndPoint.REGISTER + "/" + clientId);
    }
}
